package cn.iocoder.yudao.module.system.dal.mysql.userassetscode;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.userassetscode.vo.UserAssetsCodeUsageLogPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.userassetscode.UserAssetsCodeUsageLogDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 兑换码使用记录 Mapper
 *
 * @author 系统开发团队
 */
@Mapper
public interface UserAssetsCodeUsageLogMapper extends BaseMapperX<UserAssetsCodeUsageLogDO> {

    /**
     * 分页查询兑换码使用记录
     *
     * @param reqVO 查询条件
     * @return 分页结果
     */
    default PageResult<UserAssetsCodeUsageLogDO> selectPage(UserAssetsCodeUsageLogPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .eqIfPresent(UserAssetsCodeUsageLogDO::getCodeId, reqVO.getCodeId())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getCode, reqVO.getCode())
                .eqIfPresent(UserAssetsCodeUsageLogDO::getUserId, reqVO.getUsedByUserId())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getUsername, reqVO.getUsedByUsername())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getUserIp, reqVO.getUsedIp())
                .eqIfPresent(UserAssetsCodeUsageLogDO::getUsageResult, reqVO.getUsageResult())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getChangeName, reqVO.getPackageName())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getCustomerName, reqVO.getCustomerName())
                .likeIfPresent(UserAssetsCodeUsageLogDO::getTraceId, reqVO.getTraceId())
                .betweenIfPresent(UserAssetsCodeUsageLogDO::getUsageTime, reqVO.getUsedTimeStart(), reqVO.getUsedTimeEnd())
                .orderByDesc(UserAssetsCodeUsageLogDO::getUsageTime));
    }

    /**
     * 分页查询兑换码使用记录（兼容旧方法）
     *
     * @param userId 用户ID
     * @param username 用户名
     * @param code 兑换码
     * @param usageResult 使用结果
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 分页结果
     */
    default PageResult<UserAssetsCodeUsageLogDO> selectPage(Long userId, String username, String code, 
                                                           Integer usageResult, LocalDateTime startTime, 
                                                           LocalDateTime endTime) {
        return selectPage(new PageParam(), new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .eqIfPresent(UserAssetsCodeUsageLogDO::getUserId, userId)
                .likeIfPresent(UserAssetsCodeUsageLogDO::getUsername, username)
                .likeIfPresent(UserAssetsCodeUsageLogDO::getCode, code)
                .eqIfPresent(UserAssetsCodeUsageLogDO::getUsageResult, usageResult)
                .betweenIfPresent(UserAssetsCodeUsageLogDO::getUsageTime, startTime, endTime)
                .orderByDesc(UserAssetsCodeUsageLogDO::getUsageTime));
    }

    /**
     * 查询用户的兑换码使用记录
     *
     * @param userId 用户ID
     * @return 使用记录列表
     */
    default List<UserAssetsCodeUsageLogDO> selectByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .eq(UserAssetsCodeUsageLogDO::getUserId, userId)
                .orderByDesc(UserAssetsCodeUsageLogDO::getUsageTime));
    }

    /**
     * 查询指定时间范围内的兑换码使用记录
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 使用记录列表
     */
    default List<UserAssetsCodeUsageLogDO> selectByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return selectList(new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .between(UserAssetsCodeUsageLogDO::getUsageTime, startTime, endTime)
                .eq(UserAssetsCodeUsageLogDO::getUsageResult, 1) // 只查询成功的记录
                .orderByDesc(UserAssetsCodeUsageLogDO::getUsageTime));
    }

    /**
     * 统计用户兑换码使用次数
     *
     * @param userId 用户ID
     * @return 使用次数
     */
    default Long countByUserId(Long userId) {
        return selectCount(new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .eq(UserAssetsCodeUsageLogDO::getUserId, userId)
                .eq(UserAssetsCodeUsageLogDO::getUsageResult, 1));
    }

    /**
     * 根据兑换码ID查询使用记录
     *
     * @param codeId 兑换码ID
     * @return 使用记录列表
     */
    default List<UserAssetsCodeUsageLogDO> selectByCodeId(Integer codeId) {
        return selectList(new LambdaQueryWrapperX<UserAssetsCodeUsageLogDO>()
                .eq(UserAssetsCodeUsageLogDO::getCodeId, codeId)
                .orderByDesc(UserAssetsCodeUsageLogDO::getUsageTime));
    }

}